package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketAd;
import com.cskaoyan.market.db.domain.MarketAdExample;
import com.cskaoyan.market.db.mapper.MarketAdMapper;
import com.cskaoyan.market.service.AdminAdService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminAdServiceImpl implements AdminAdService {

    @Autowired
    MarketAdMapper marketAdMapper;

    @Override
    public List<MarketAd> list(Integer page, Integer limit, String sort, String order, String name, String content) {

        MarketAdExample marketAdExample = new MarketAdExample();
        MarketAdExample.Criteria criteria = marketAdExample.createCriteria();
        if(!StringUtils.isEmpty(name)){
            // select * from sxxx where username like '%xxx%'
            criteria.andNameLike("%" + name + "%");
        }
        if(!StringUtils.isEmpty(content)){
            criteria.andContentLike("%" + content + "%");
        }
        page=1;
        limit=20;
        marketAdExample.setOrderByClause(sort+" "+order);
        PageHelper.startPage(page,limit);
        List<MarketAd> marketAds = marketAdMapper.selectByExample(marketAdExample);
        return marketAds;
    }

    @Override
    public int delete(Integer id) {
        int i = marketAdMapper.deleteByPrimaryKey(id);
        return i;
    }

    @Override
    public int update(MarketAd marketAd) {
        int i = marketAdMapper.updateByPrimaryKey(marketAd);
        return i;
    }

    @Override
    public int create(MarketAd marketAd) {
        int i = marketAdMapper.insert(marketAd);
        return i;
    }


}
